#!/bin/bash
#######################################################################
# Copyright (c): 2012-2022, Huawei Tech. Co., Ltd.
# description: construct huaweijdk
# version: 1.0.0
# history:
#######################################################################
set -e

SOURCE_FILE='jdk'
rm -rf ./"${SOURCE_FILE}"

ARCH=$(uname -m)
IFS=$(echo -en "\n\b")
LOCAL_PATH=${0}
FIRST_CHAR_PATH=$(expr substr "$LOCAL_PATH" 1 1)
if [ "$FIRST_CHAR_PATH" = "/" ]; then
    LOCAL_PATH=${0}
else
    LOCAL_PATH="$(pwd)/${LOCAL_PATH}"
fi
LOCAL_DIR=$(dirname "${LOCAL_PATH}")
ROOT_DIR="${LOCAL_DIR}/../.."
TARGET_PATH=${ROOT_DIR}/output/kernel/platform/openjdk8
[ -d ${TARGET_PATH} ] && rm -rf ${TARGET_PATH}/*

mkdir -pv ${TARGET_PATH}

if [ ${ARCH} = "aarch64" ];then
    TAR_SOURCE_FILE="bisheng-jdk-8u312-linux-aarch64.tar.gz";
elif [ ${ARCH} = "x86_64" ];then
    TAR_SOURCE_FILE="bisheng-jdk-8u312-linux-x64.tar.gz";
elif [ ${ARCH} = "loongarch64" ];then
    TAR_SOURCE_FILE="loongson8.1.10-jdk8u312b07-linux-loongarch64.tar.gz";
else
    echo "There is no jdk for the architecture $ARCH"
    exit 1
fi

mkdir ${SOURCE_FILE}
tar -zxvf "${TAR_SOURCE_FILE}" -C $SOURCE_FILE --strip-components 1
chmod -R 700 $SOURCE_FILE
cp -a ./"${SOURCE_FILE}" ${TARGET_PATH}

COPY_PATH_TARGET=${ROOT_DIR}/output/kernel/platform/openjdk8/${ARCH}
if [ ! -d ${COPY_PATH_TARGET} ]; then
    mkdir -pv $COPY_PATH_TARGET
    cp -a ./"${SOURCE_FILE}" "${COPY_PATH_TARGET}/jdk"
fi

